﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using ThermalEquipmentDesign.ViewModel;
using System.Media;
using System.Windows.Controls.Primitives;
namespace ThermalEquipmentDesign.View.CoolerWet
{
    /// <summary>
    /// Interaction logic for ucCoolerWet.xaml
    /// </summary>
    public partial class ucCoolerWet : UserControl
    {
        String key;
        ToolTip tt = new ToolTip();
        public CoolerWetViewModel CoolerWetViewModel = new CoolerWetViewModel();

        public ucCoolerWet()
        {
            InitializeComponent();            
            this.DataContext = CoolerWetViewModel;
            tt.Content = CoolerWetViewModel.sTooltip;            
        }
        #region EMPTY VALUE
        public void CheckEmpty(object sender, TextChangedEventArgs e)
        {
            TextBox txt = (TextBox)sender;
            if (txt.Text == "")
            {
                txt.Text = "0";
            }
            if (txt.Text.First() == '.')
            {
                txt.Text = txt.Text.Remove(0, 1);
                SystemSounds.Beep.Play();
            }
            if (txt.Text.IndexOf('-') != -1 && txt.Text.IndexOf('-') != 0)
            {
                SystemSounds.Beep.Play();
            }
        }
        #endregion

        #region CHECK INPUT
        private void txtInput_PreviewTextInput(object sender, TextCompositionEventArgs e)
        {
            if (Char.IsDigit((Char)e.Text[0]))
            {
                e.Handled = false;
            }
            else
            {
                if ((Char)e.Text[0] == '.')
                {
                    if (!((TextBox)sender).Text.Contains("."))
                    {
                        e.Handled = false;
                    }
                    else
                    {
                        e.Handled = true;
                        SystemSounds.Beep.Play();
                    }
                }
                else
                {
                    if ((Char)e.Text[0] == '-')
                    {
                        if (!((TextBox)sender).Text.Contains("-"))
                        {
                            e.Handled = false;
                        }
                        else
                        {
                            e.Handled = true;
                            SystemSounds.Beep.Play();
                        }
                    }
                    else
                    {
                        e.Handled = true;
                        SystemSounds.Beep.Play();
                    }
                }
            }
        }

        private void txtInput_PreviewKeyDown(object sender, KeyEventArgs e)
        {
            TextBox txt = (TextBox)sender;
            if (e.Key == Key.Space && txt.IsFocused == true)
            {
                e.Handled = true;
                SystemSounds.Beep.Play();
            }
        }
        #endregion

        #region GOT FOCUS
        private void txtInput_GotFocus(object sender, RoutedEventArgs e)
        {
            TextBox txt = (TextBox)sender;
            key = txt.Name;
        }

        private void txtInput_Loaded(object sender, RoutedEventArgs e)
        {
            TextBox txt = (TextBox)sender;
            ToolTipService.SetPlacement(tt, PlacementMode.Left);
            txt.ToolTip = tt;
        }
        #endregion

        private void grid1_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.F1)
            {
                CoolerWetViewModel.LoadReference(key);
            }
        }
    }
}
